from LeetCoder import *


class Solution:

    def semiOrderedPermutation(self, nums: List[int]) -> int:
        n = len(nums)
        a, b = 0, 0
        for i, v in enumerate(nums):
            if v == 1:
                a = i
            if v == n:
                b = i
        ans = a + n - b - 1
        if a > b:
            ans -= 1

        return ans


s = Solution()
print(s)
